package com.lianqi.emcpframework.extend.script.engine;


import org.springframework.util.DigestUtils;

import javax.script.CompiledScript;

/**
 * @program emcp-framework
 * @ClassName JavaScriptEngine
 * @description: js 脚本解析引擎
 * @author: sky
 * @create: 2018/12/05 11:20
 */
public class JavaScriptEngine extends CommonScriptEngine{

    static final String NAME = "javascript";
    @Override
    public String getScriptName() {
        return NAME;
    }

    @Override
    public boolean compile(String id, String script) throws Exception {

        if (compilable == null){
            init();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("(function(){").append(script).append("\n})();");
        CompiledScript compiledScript = compilable.compile(sb.toString());
        CommonScriptContext context = new CommonScriptContext(id,
                DigestUtils.md5DigestAsHex(script.getBytes()),compiledScript);
        return true;
    }
}
